/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;

import javax.swing.JPanel;

import algo.Algo;
import algo.Chemin;
import algo.Ville;

public class CarteFourmi extends JPanel {

	private static final long serialVersionUID = 1L;
	private Algo algo;
	private static final int UNITE_DISTANCE = 100;

	public CarteFourmi(Algo algo) {
		super();
		this.algo = algo;
		this.setBackground(new Color(30, 30, 30));
		this.setMinimumSize(new Dimension(100, 100));
		this.setPreferredSize(new Dimension(200, 200));

	}

	public float largeurPheromone(Chemin chemin) {

		double nombre = chemin.getDensitePheromone() * 2;
		return (float) (nombre);
	}

	public void dessineChemins(Graphics g) {

		Graphics2D g2d = (Graphics2D) g;
		g.setColor(new Color(0, 255, 80));
		ArrayList<Chemin> chemins = algo.getLesChemins();

		float width = 1;
		for (Chemin chemin_courant : chemins) {
			width = largeurPheromone(chemin_courant);
			g2d.setStroke(new BasicStroke(width));
			g.drawLine(chemin_courant.getVilleA().getX() * UNITE_DISTANCE,
					chemin_courant.getVilleA().getY() * UNITE_DISTANCE,
					chemin_courant.getVilleB().getX() * UNITE_DISTANCE,
					chemin_courant.getVilleB().getY() * UNITE_DISTANCE);
		}

	}

	public void dessineParcours(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g.setColor(new Color(255, 0, 0));

		ArrayList<Chemin> parcours = algo.getParcours_record();
		float width;
		if (parcours != null) {
			for (Chemin chemin_courant : parcours) {
				width = largeurPheromone(chemin_courant);
				g2d.setStroke(new BasicStroke(width));
				g.drawLine(chemin_courant.getVilleA().getX() * UNITE_DISTANCE,
						chemin_courant.getVilleA().getY() * UNITE_DISTANCE,
						chemin_courant.getVilleB().getX() * UNITE_DISTANCE,
						chemin_courant.getVilleB().getY() * UNITE_DISTANCE);
			}
		}
	}

	public void dessineVilles(Graphics g) {

		Graphics2D g2d = (Graphics2D) g;
		ArrayList<Ville> liste = algo.getMesVilles();
		for (Ville ville_courante : liste) {
			g2d.setColor(Color.RED);
			g2d.fillOval((ville_courante.getX() * UNITE_DISTANCE) - 5,
					(ville_courante.getY() * UNITE_DISTANCE) - 5, 10, 10);

			g2d.setColor(Color.BLACK);
			g2d.drawOval((ville_courante.getX() * UNITE_DISTANCE) - 5,
					(ville_courante.getY() * UNITE_DISTANCE) - 5, 10, 10);

			g2d.setColor(Color.WHITE);
			g2d.drawString(ville_courante.getNom(),
					(ville_courante.getX() * UNITE_DISTANCE) + 20,
					(ville_courante.getY() * UNITE_DISTANCE) - 5);

		}

	}

	@Override
	public void paintComponent(Graphics g) {

		super.paintComponent(g);
		dessineChemins(g);
		dessineVilles(g);
		dessineParcours(g);

	}
}
